package hcom.model.main.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import hcom.model.board.bean.BoardBean;
import hcom.model.board.dao.BoardDao;
import hcom.model.main.bean.MainBean;
import hcom.model.main.dao.MainDao;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class MainAction extends ActionSupport implements Preparable, ModelDriven<MainBean> {
	
	private MainBean mainBean;
	private List<MainBean> ipList;
	private BoardDao dao = BoardDao.getInstance();
	private MainBean adminBean;
	private MainBean leaderBean;
	private List<BoardBean> list;
	private List<BoardBean> newlist;
	private List<BoardBean> liblist;
	
	public String checkIP() throws Exception {
		String result = null;
		ActionContext context = ActionContext.getContext();
		Map<String, String> session = (Map<String,String>)context.getSession();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		ipList = MainDao.getInstance().checkIP(request.getRemoteAddr());
		
		if(ipList.size() == 0){
			result = "FAIL";
		} else if(ipList.size() > 0) {
			adminBean = MainDao.getInstance().getAdminInfo(mainBean.getId()); 
			
			if(adminBean != null) {
				session.put("position_flag", "admin");
			} else {
				leaderBean = MainDao.getInstance().getLeaderInfo(mainBean.getId());
				
				if(leaderBean != null) {
					session.put("position_flag", "leader");
				} else {
					session.put("position_flag", "employee");
				}
			}

			//String name = new String(mainBean.getName().getBytes("ISO-8859-1"),"UTF-8");
			System.out.println(mainBean.getName());
			session.put("id", mainBean.getId());
			session.put("name", mainBean.getName());
			session.put("email", mainBean.getEmail());
			session.put("detp", mainBean.getDept());
			session.put("position", mainBean.getPosition());
			
			//mian 게시글 가져오기 
			list = dao.getBoardList("BD100", null, null, 0, 7); 
			newlist = dao.getnewBoardList(); 
			liblist = dao.getlibBoardList(); 
			
			// 로그인 로그 저장
			mainBean.setIp_address(request.getRemoteAddr());
			mainBean.setMem_no(mainBean.getId());
			mainBean.setMem_dept(mainBean.getDept());
			mainBean.setMem_position(mainBean.getPosition());
			mainBean.setMem_name(mainBean.getName());
			System.out.println("**"+mainBean.getMem_name());
			MainDao.getInstance().insertLoginLog(mainBean);
			
			result = "SUCCESS";
		}
		
		return result;
	}

	
	
	public List<BoardBean> getLiblist() {
		return liblist;
	}
	
	public List<BoardBean> getNewlist() {
		return newlist;
	}

	public List<BoardBean> getList() {
		return list;
	}

	@Override
	public MainBean getModel() {
		// TODO Auto-generated method stub
		return mainBean;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		mainBean = new MainBean();
	}

	public MainBean getMainBean() {
		return mainBean;
	}

	public void setMainBean(MainBean mainBean) {
		this.mainBean = mainBean;
	}

	public List<MainBean> getIpList() {
		return ipList;
	}

	public void setIpList(List<MainBean> ipList) {
		this.ipList = ipList;
	}

	public MainBean getAdminBean() {
		return adminBean;
	}

	public void setAdminBean(MainBean adminBean) {
		this.adminBean = adminBean;
	}

	public MainBean getLeaderBean() {
		return leaderBean;
	}

	public void setLeaderBean(MainBean leaderBean) {
		this.leaderBean = leaderBean;
	}
}
